package com.slz3.business.module.configManagement.conSiteCounty.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.configManagement.conSiteCounty.domain.ConSiteCounty;
import com.slz3.business.module.configManagement.conSiteCounty.domain.ConSiteCountyFactory;
import com.slz3.business.module.configManagement.conSiteCounty.steward.ConSiteCountySpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class ConSiteCountyServiceImpl implements ConSiteCountyService {

    @Override
    public ConSiteCounty save(ConSiteCounty conSiteCounty) {
        return Repositorys.conSiteCounty.save(ConSiteCountyFactory.initBaseFields(conSiteCounty));
    }

    @Override
    public Page<ConSiteCounty> page(MultiValueMap<String, String> params) {
        Page<ConSiteCounty> conSiteCountyPage = Repositorys.conSiteCounty.findAll(
                ConSiteCountySpecification.findJoin(params), PageableSteward.extract(params)
        );
        return conSiteCountyPage;
    }

    @Override
    public ConSiteCounty modify(ConSiteCounty conSiteCountyNew) {
        return Repositorys.conSiteCounty.saveAndFlush(ConSiteCountyFactory.initBaseFields(conSiteCountyNew));
    }

    @Override
    public Optional<ConSiteCounty> single(UUID uuid) {
        return Repositorys.conSiteCounty.findById(uuid);
    }

    @Override
    public List<ConSiteCounty> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.conSiteCounty.findAllById(uuids);
    }

    @Override
    public List<ConSiteCounty> delete(List<ConSiteCounty> conSiteCountyList) {
        conSiteCountyList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            ConSiteCountyFactory.initBaseFields(item);
        });
        return Services.conSiteCountyTransactional.batchModify(conSiteCountyList);
    }

}
